﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Config;
using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.Task.Entity;
using PowerCreator.LMS.Web.Task;


public partial class Theme_3_OpenCourseWareCommentTask : PowerCreator.LMS.Web.Base.BasePage
{
    protected string fileUrl = string.Empty;
    protected string url = string.Empty;
    protected string ResourceID = string.Empty;
    protected string _Position = "0";
    protected string taskCommentUrl = string.Empty;
    protected string AddTaskCommentUrl = string.Empty;
    protected string strGrade = "<a href=\"#\" onclick=\"checkGrade({0})\">{1}</a>";

    protected string isUpdateCourse = "1";  //0不更新课件信息,1更新课件信息

    const string _openhistoryurl = "/web/Project/OpenHistory.aspx?ProjectID={0}&ProjectClassID={1}&ProjectClassUserID={2}&ProjectResourceID={3}&CourseResourceID={4}&ResourceID={5}&ProjectResourceResourceStartTime={6}";



    const string IMG_OK = "<img src=\"images/x1.jpg\" width=\"16\" height=\"12\" />";
    const string IMG_NO = "<img src=\"images/x2.jpg\" width=\"16\" height=\"12\" />";

    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    protected string AddCommentUrl = "OpenCourseWareCommentNew.aspx?ProjectID={0}&ClassID={1}&UserID={2}&ProjectResourceID={3}&CourseResourceID={4}&ResourceID={5}&Position={6}&CourseID={7}";

    PowerCreator.LMS.Web.Comment.Comment opComment = new PowerCreator.LMS.Web.Comment.Comment();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.Web.Course.Resource opResource = new PowerCreator.LMS.Web.Course.Resource();
    PowerCreator.LMS.ViewQuery.ViewQuery objViewQuery = new PowerCreator.LMS.ViewQuery.ViewQuery();

    TaskCommentManag objTaskCommentManag = new TaskCommentManag();
    EvaluateManag objEvaluateManag = new EvaluateManag();
    TaskManag objTaskManag = new TaskManag();
    VirtualDirectory objVirtualDirectory = new VirtualDirectory();

    PowerCreator.LMS.Web.Course.VirtualDirectory opVirtual = new VirtualDirectory();
    protected string _ResourceID = "";
    ManagerPaper opPaper = new ManagerPaper();
    private PowerCreator.LMS.Web.User.LoginEntity login = new PowerCreator.LMS.Web.User.LoginEntity();
    private string Loginurl = "UserLogin.aspx";


    //protected string _userid = "0";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (!IsPostBack)
        {
            //this.Title = config.GetLanagerValueByKey("theme_Comment_TitleInfo", PowerCreator.LMS.Language.LanagetItemType.Page);

            if (Request.Params["TaskID"] != null && Request.Params["TaskID"].Trim() != "")
            {
                int TaskID = int.Parse(Request.Params["TaskID"].ToString());
                ViewState["TaskID"] = TaskID;

            }
            if (Request.Params["TaskClassID"] != null && Request.Params["TaskClassID"].Trim() != "")
            {
                int TaskClassID = int.Parse(Request.Params["TaskClassID"].ToString());
                ViewState["TaskClassID"] = TaskClassID;

            }
            if (Request.Params["TaskUserID"] != null && Request.Params["TaskUserID"].Trim() != "")
            {
                int TaskUserID = int.Parse(Request.Params["TaskUserID"].ToString());
                ViewState["TaskUserID"] = TaskUserID;

            }
            if (Request.Params["ResourceID"] != null && Request.Params["ResourceID"].Trim() != "")
            {
                int ResourceID = int.Parse(Request.Params["ResourceID"].ToString());
                ViewState["ResourceID"] = ResourceID;

            }

            taskCommentUrl=string.Format("TaskID={0}&TaskClassID={1}&TaskUserID={2}&ResourceID={3}&ContentID=0", ViewState["TaskID"].ToString(), ViewState["TaskClassID"].ToString(), ViewState["TaskUserID"].ToString(), ViewState["ResourceID"].ToString());
            AddTaskCommentUrl = string.Format("&TaskID={0}&TaskClassID={1}&TaskUserID={2}&ResourceID={3}", ViewState["TaskID"].ToString(), ViewState["TaskClassID"].ToString(), ViewState["TaskUserID"].ToString(), ViewState["ResourceID"].ToString());

            BinderUserLogin();
            btn_out.Text = config.GetLanagerValueByKey("SignOut", PowerCreator.LMS.Language.LanagetItemType.WebTitle);

        
            BinderPG();
         
            SetOpenHistoryUrl();

        }
        if (Request.QueryString["TimePosition"] != null)
        {
            ViewState["TimePosition"] = Request.QueryString["TimePosition"].ToString();
        }

    }


    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(Loginurl);
        }

        login = (PowerCreator.LMS.Web.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(Loginurl);
        }

    }


    protected void lnkbtn_EN_Click(object sender, EventArgs e)
    {
        Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_LANGEID] = "1";
    }
    protected void lnkbtn_CN_Click(object sender, EventArgs e)
    {
        Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_LANGEID] = "0";
    }
    protected void BinderUserLogin()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            lal_UserLogin.Text = "<a  href=\"UserLogin.aspx\">" + config.GetLanagerValueByKey("SysManagerLogin", PowerCreator.LMS.Language.LanagetItemType.Page) + "</a> |  <a  href=\"Register.aspx\">" + config.GetLanagerValueByKey("SysManagerRegistor", PowerCreator.LMS.Language.LanagetItemType.Page) + "</a>";
            btn_out.Visible = false;
        }
        else
        {
            PowerCreator.LMS.Web.User.LoginEntity login = (PowerCreator.LMS.Web.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            lal_UserLogin.Text = login.AccountInfo.RealName.Trim() + "," + config.GetLanagerValueByKey("Login_msg_code5", PowerCreator.LMS.Language.LanagetItemType.Page) + "！| ";
            btn_out.Visible = true;
        }
    }
    protected void btn_out_Click(object sender, EventArgs e)
    {
        PowerCreator.LMS.Web.User.User opUser = new PowerCreator.LMS.Web.User.User();
        //opUser.UpdateOnlineUserByUserID(GetLoginEntity.UserID);
        Session.Abandon();
        PowerCreator.LMS.Common.Script.Script.ReloadMe();
    }

    private void BinderPG()
    {
        //初始化界面的播放地址
        PowerCreator.LMS.Course.Entity.VOTResource voResource = opCourse.LoadResource(int.Parse(ViewState["ResourceID"].ToString()));
        lbl_ResourceName.Text = voResource.ResourceName;
        lab_ClickNum.Text = voResource.ResourceClickNum.ToString();

        if (voResource.ResourceID < 0)
        {
            Script.ShowAndCloseMe(Page, config.GetLanagerValueByKey("theme_Comment_OpenCourseware_Msg1", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }

        int userNum = 0;        //评价总人数
        int ScoreNum=0;         //评价总分数
        int SumScore = 0;       //量表的总分数
        DataTable dtTaskComment = new DataTable();

        string searchTaskComment = string.Format(" TaskCtTaskID={0} and TaskCtTaskClassID={1} and TaskCtUserID={2} and TaskCtType=4 ", ViewState["TaskID"].ToString(), ViewState["TaskClassID"].ToString(), ViewState["TaskUserID"].ToString());
        dtTaskComment=objTaskCommentManag.Query(searchTaskComment);

        for (int i = 0; i < dtTaskComment.Rows.Count; i++)
		{
            int score=int.Parse(dtTaskComment.Rows[i]["TaskCtScore"].ToString());
            ScoreNum+=score;
		}

        userNum =dtTaskComment.Rows.Count;
        if (userNum > 0)
        {
            lab_AvgScore.Text = Convert.ToString(ScoreNum / userNum);

        }
        else
        {
            lab_AvgScore.Text = "0";
        }

        VOTask objVOTask= objTaskManag.Load(int.Parse(ViewState["TaskID"].ToString()));
       ViewState["EvateID"]=objVOTask.FKTaskEvaluateID;
        ViewState["TaskAppraiseType"]= objVOTask.TaskAppraiseType;
        SumScore = objEvaluateManag.Load(objVOTask.FKTaskEvaluateID).EvateScore;


        string str_Star = "";
        if (objVOTask.TaskAppraiseType == 2)//量表评价
        {
            strGrade = string.Format(strGrade, "target", GetLanageValueByKey("theme_Comment_AddComment", PowerCreator.LMS.Language.LanagetItemType.Page));

            double result = Convert.ToDouble(lab_AvgScore.Text) / SumScore;
            int countStart = Convert.ToInt32(result * 10);

            for (int i = 0; i < 10; i++)
            {

                if (i < countStart)
                {
                    str_Star += IMG_OK;
                }
                else
                {
                    str_Star += IMG_NO;
                }


            }
        }
        else//直接打分
        {
            strGrade = string.Format(strGrade, "mark", GetLanageValueByKey("theme_Comment_AddComment", PowerCreator.LMS.Language.LanagetItemType.Page));
            int countStart = 7;//暂时设定为7颗星
            for (int i = 0; i < 10; i++)
            {

                if (i < countStart)
                {
                    str_Star += IMG_OK;
                }
                else
                {
                    str_Star += IMG_NO;
                }


            }
 
        }
        lab_Star.Text = str_Star;
        //if (!CheckFile(voResource)) return;
        if (PowerCreator.LMS.Web.Config.WebConfig.IsUseLmsModel)
        {
            string url = PowerCreator.LMS.Common.Utils.WebUtils.WebRoot + voResource.ResourceUrl + voResource.ResourceEntry;
            fileUrl = url;
            if (voResource.ResourceClass == Convert.ToInt16(SysResourceType.PowerCreator))
            {
                fileUrl = PowerCreator.LMS.Common.Utils.WebUtils.WebRoot + voResource.ResourceUrl;
                //fileUrl = "../Single/content.aspx?fileUrl=" + fileUrl;
                fileUrl = string.Format("../Single/content.aspx?rsd={0}", ViewState["ResourceID"]);

            }
            else if (voResource.ResourceClass == Convert.ToInt16(SysResourceType.Video))
            {

                fileUrl = string.Format("../Video/OpenVideo.aspx?rsd={0}", ViewState["ResourceID"]);
            }

        }
        else
        {

            string url = PowerCreator.LMS.Common.Utils.WebUtils.WebRoot + voResource.ResourceUrl + voResource.ResourceEntry;
            fileUrl = url;
            if (voResource.ResourceClass == Convert.ToInt16(SysResourceType.PowerCreator))
            {
            }
            else if (voResource.ResourceClass == Convert.ToInt16(SysResourceType.Video))
            {
                string ServerIP;
                if (PowerCreator.LMS.Web.Config.WebConfig.IsUseURLIP)
                {
                    int index = Request.Url.Authority.IndexOf(":", 0);
                    if (index != -1)
                    {
                        ServerIP = Request.Url.Authority.Remove(index);
                    }
                    else
                    {
                        ServerIP = Request.Url.Authority;
                    }

                    fileUrl = "mms://" + ServerIP + voResource.ResourceUrl + voResource.ResourceEntry;
                }
                else
                {
                    PowerCreator.LMS.ViewQuery.ViewQuery opView = new PowerCreator.LMS.ViewQuery.ViewQuery();
                    string sDirName = opResource.GetDirctoryName(voResource.ResourceUrl);


                    string search = string.Format(" VirIisName={0}",sDirName);


                    DataTable dtDir = objVirtualDirectory.QueryVirtual(search);
                    if (dtDir.Rows.Count == 1)
                    {
                        ServerIP = dtDir.Rows[0]["VirIp"].ToString();
                        string sResourceMMSUrl = voResource.ResourceUrl.Replace(sDirName, dtDir.Rows[0]["VirMmsName"].ToString());
                        fileUrl = "mms://" + ServerIP + sResourceMMSUrl + voResource.ResourceEntry;
                    }
                }
                fileUrl = "OpenVideo.aspx?fileUrl=" + fileUrl;

            }

        }
    }
    /// <summary>
    /// 检测文件是否存在
    /// </summary>
    /// <returns></returns>
    private bool CheckFile(PowerCreator.LMS.Course.Entity.VOTResource info)
    {
        string path = info.ResourceUrl + info.ResourceEntry;

        if (opResource.ExistsResourcePath(path))
        {
            //isUpdateCourse = "1";
            return true;
        }
        else
        {
            isUpdateCourse = "0";
            //是否创建一个界面，用来提交该处的课件信息?      skey 2009-7-20
            Script.ShowAndCloseMe(Page, config.GetLanagerValueByKey("theme_Comment_OpenCourseware_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
    }

    private void SetOpenHistoryUrl()
    {
        url = PowerCreator.LMS.Common.Utils.WebUtils.WebRoot + string.Format(_openhistoryurl, ViewState["ProjectID"], ViewState["ClassID"], GetLoginEntity.UserID.ToString(), ViewState["ProjectResourceID"], ViewState["CoursResourceID"], ViewState["ResourceID"], DateTime.Now.ToString());
    }

    private string GetPicName(string fileName, int Resourceclass)
    {
        string rt = "images/Exam.jpg";
        if (Resourceclass == 1)
        {
            rt = "000.jpg";
        }
        else if (Resourceclass == 2)
        {
            string[] fileNames = fileName.Split('.');
            if (fileNames.Length == 2)
                rt = fileNames[0].ToString() + ".jpg";
        }
        return rt;
    }
    private string GetPath(string Path, int Resourceclass)
    {
        string rt = Path;
        if (Resourceclass != 1 && Resourceclass != 2)
        {
            rt = "";
        }
        return rt;
    }
}
